//
//  UIView+WDSD.m
//  WDLoan
//
//  Created by 邓乐 on 2016/10/10.
//  Copyright © 2016年 wanda. All rights reserved.
//

#import "UIView+WDSD.h"
#import <objc/runtime.h>

static const void * WDVIEW_SIDE_BORDER_LAYER_KEY = &WDVIEW_SIDE_BORDER_LAYER_KEY;
static const void * WDVIEW_CORNER_BORDER_LAYER_KEY = &WDVIEW_CORNER_BORDER_LAYER_KEY;
static NSString *tap_touch_block = @"tap_touch_block";

@implementation UIView (WDSD)

- (void)setBorders:(NSArray *)parameters Width:(CGFloat)width color:(UIColor *)color {
    
    CAShapeLayer *shape = [self getSideBorderLayer];
    
    CGMutablePathRef ms = CGPathCreateMutable();
    
    for (NSArray *param in parameters) {
        
        if ([param count] < 3) {
            continue;
        }
        WDViewBorder border = [param[0] intValue];
        CGFloat margin1 = [param[1] floatValue];
        CGFloat margin2 = [param[2] floatValue];
        
        if (WDViewBorderTop & border) {
            CGPathAddRect(ms, &CGAffineTransformIdentity, CGRectMake(margin1, 0, VW(self) - (margin1 + margin2), width));
        }
        
        else if (WDViewBorderLeft & border) {
            CGPathAddRect(ms, &CGAffineTransformIdentity, CGRectMake(0, margin1, width, VH(self) - (margin1 + margin2)));
        }
        
        else if (WDViewBorderBottom & border) {
            CGPathAddRect(ms, &CGAffineTransformIdentity, CGRectMake(margin1, VH(self) - width, VW(self) - (margin1 + margin2), width));
        }
        
        else if (WDViewBorderRight & border) {
            CGPathAddRect(ms, &CGAffineTransformIdentity, CGRectMake(VW(self) - width, margin1, width, VH(self) - (margin1 + margin2)));
        }
    }
    shape.path = ms;
    CGPathRelease(ms);
    shape.fillColor = color.CGColor;
}

- (void)setBorder:(WDViewBorder)border Width:(CGFloat)width Color:(UIColor*)color {
    [self setBorder:border Width:width margin:0.0f radius:0.0f color:color];
}

- (CAShapeLayer *)getSideBorderLayer {
    return [self lazyCreateShapeLayer:WDVIEW_SIDE_BORDER_LAYER_KEY];
}

- (CAShapeLayer *)getCornerBorderLayer {
    return [self lazyCreateShapeLayer:WDVIEW_CORNER_BORDER_LAYER_KEY];
}

- (CAShapeLayer *)lazyCreateShapeLayer:(const void *)key {
    CAShapeLayer *layer = objc_getAssociatedObject(self, key);
    if (!layer) {
        layer = [CAShapeLayer layer];
        objc_setAssociatedObject(self, key, layer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        [self.layer addSublayer:layer];
    }
    return layer;
}

- (void)removeShapeLayer:(const void *)key {
    CAShapeLayer *layer = objc_getAssociatedObject(self, key);
    if (layer && layer.superlayer) {
        [layer removeFromSuperlayer];
    }
    if (layer) {
        objc_setAssociatedObject(self, key, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
}

- (void)setBorder:(WDViewBorder)border Width:(CGFloat)width margin:(CGFloat)margin radius:(CGFloat)radius color:(UIColor *)color {
    CAShapeLayer *shape = [self getSideBorderLayer];
    
    CGMutablePathRef ms = CGPathCreateMutable();
    
    if (WDViewBorderTop & border) {
        CGPathAddRect(ms, &CGAffineTransformIdentity, CGRectMake(margin, 0, VW(self) - 2 * margin, width));
    }
    
    if (WDViewBorderLeft & border) {
        CGPathAddRect(ms, &CGAffineTransformIdentity, CGRectMake(0, margin, width, VH(self) - 2 * margin));
    }
    
    if (WDViewBorderBottom & border) {
        CGPathAddRect(ms, &CGAffineTransformIdentity, CGRectMake(margin, VH(self) - width, VW(self) - 2 * margin, width));
    }
    
    if (WDViewBorderRight & border) {
        CGPathAddRect(ms, &CGAffineTransformIdentity, CGRectMake(VW(self) - width, margin, width, VH(self) - 2 * margin));
    }
    
    shape.path = ms;
    CGPathRelease(ms);
    shape.fillColor = color.CGColor;
    
    if (radius > 0) {
        CAShapeLayer *shape1 = [self getCornerBorderLayer];
        
        CGMutablePathRef ms1 = CGPathCreateMutable();
        
        CGFloat margin = width/2;
        
        if ((WDViewBorderTop & border) && (WDViewBorderLeft & border)) {
            CGPathMoveToPoint(ms1, &CGAffineTransformIdentity, margin, radius);
            CGPathAddArcToPoint(ms1, &CGAffineTransformIdentity, margin, margin, radius, margin, radius);
        }
        if ((WDViewBorderTop & border) && (WDViewBorderRight & border)) {
            CGPathMoveToPoint(ms1, &CGAffineTransformIdentity, VW(self) - margin, radius);
            CGPathAddArcToPoint(ms1, &CGAffineTransformIdentity, VW(self) - margin, margin, VW(self) - radius, margin, radius);
        }
        if ((WDViewBorderBottom & border) && (WDViewBorderRight & border)) {
            CGPathMoveToPoint(ms1, &CGAffineTransformIdentity, VW(self) - margin, VH(self) - radius);
            CGPathAddArcToPoint(ms1, &CGAffineTransformIdentity, VW(self) - margin, VH(self) - margin, VW(self) - radius, VH(self) - margin, radius);
        }
        if ((WDViewBorderBottom & border) && (WDViewBorderLeft & border)) {
            CGPathMoveToPoint(ms1, &CGAffineTransformIdentity, margin, VH(self) - radius);
            CGPathAddArcToPoint(ms1, &CGAffineTransformIdentity, margin, VH(self) - margin, radius, VH(self) - margin, radius);
        }
        shape1.path = ms1;
        CGPathRelease(ms1);
        shape1.lineWidth = width;
        shape1.strokeColor = color.CGColor;
        shape1.fillColor = [UIColor clearColor].CGColor;
    }
}


- (void)removeBorder {
    [self removeShapeLayer:WDVIEW_SIDE_BORDER_LAYER_KEY];
    [self removeShapeLayer:WDVIEW_CORNER_BORDER_LAYER_KEY];
}

- (void)addActionWithBlock:(UITouchBlock)block{
    if (block) {
        objc_setAssociatedObject(self, &tap_touch_block, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
        self.userInteractionEnabled = YES;
        [self addTarget:self action:@selector(invoke)];
    }
}

- (void)addTarget:(id)target action:(SEL)action{
    self.userInteractionEnabled = YES;
    //移除其他点击手势
    [self.gestureRecognizers enumerateObjectsUsingBlock:^(__kindof UIGestureRecognizer * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([obj isMemberOfClass:[UITapGestureRecognizer class]]) {
            [self removeGestureRecognizer:obj];
        }
    }];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:target action:action];
    [self addGestureRecognizer:tap];
}

- (void)invoke{
    UITouchBlock block = objc_getAssociatedObject(self, &tap_touch_block);
    if (block) {
        block();
    }
}

@end
